package com.kky.pp.activity.login.phone

import android.os.Handler
import android.text.InputType
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.constraintlayout.widget.ConstraintLayout
import com.airbnb.lottie.LottieAnimationView
import com.kky.pp.R
import com.kky.pp.base.TitleMvpBaseActivity
import com.kky.pp.view.codeview.VerificationCodeView
import com.custom.base.view.SendCodeView
import com.gyf.immersionbar.ImmersionBar
import com.kky.pp.databinding.ActivityPhoneLoginBinding
import com.kky.pp.util.KeyboardUtils
import com.kky.pp.util.LocalTestUtils
import com.kky.pp.util.SoftInputUtils


class PhoneLoginActivity : TitleMvpBaseActivity<PhoneLoginContract.View, PhoneLoginPresenter,ActivityPhoneLoginBinding>(ActivityPhoneLoginBinding::inflate),
    PhoneLoginContract.View {

    private val phoneKeyboardUtils by lazy { KeyboardUtils() }
    private val codeKeyboardUtils by lazy { KeyboardUtils() }
    override fun setFullScreen(): Boolean {
        return true
    }

    override val showBack: Boolean
        get() = true

    override fun bindLayout(): Int {
        return R.layout.activity_phone_login
    }

    override fun initView() {
        initListener()
    }

    private fun initListener() {
        codeKeyboardUtils.registerKeyboardHeightListener(this@PhoneLoginActivity,view = codeContainer)
        phoneKeyboardUtils.registerKeyboardHeightListener(this@PhoneLoginActivity,view = phoneNextContainer)
        if (LocalTestUtils.isDebug()){
            editPhone.setText("6505551111")
            for (str in 1.. 6){
                editCode.setText(str.toString())
            }
        }else{
            Handler().postDelayed(Runnable {
                //google手机 activity启动软键盘弹出 测算软键盘高度会有误差，延迟弹出软键盘可规避该问题
                SoftInputUtils.showSoftInput(editPhone, InputType.TYPE_CLASS_NUMBER)
            },200)
        }


    }

    override fun onResume() {
        super.onResume()
        ImmersionBar.with(mActivity).statusBarDarkFont(false).init()
    }
    override fun onBackListener() {
        mPresenter?.onBackListener()
    }

    override val editPhone: EditText
        get() = viewBinding.editPhoneDLInput
    override val phoneNext: TextView
        get() = viewBinding.txtPhoneDLNext
    override val phoneOutsideContainer: ConstraintLayout
        get() = viewBinding.conPhoneDLNumberOutsideContainer
    override val codeOutsideContainer: ConstraintLayout
        get() = viewBinding.conPhoneDLCodeOutsideContainer
    override val textLoginPhone: TextView
        get() = viewBinding.txtPhoneDLLoginPhone
    override val editCode: VerificationCodeView
        get() = viewBinding.txtPhoneDLVerCode
    override val actLoginCode: SendCodeView
        get() = viewBinding.txtPhoneDLSendVerCode
    override val codeNext: TextView
        get() = viewBinding.txtPhoneDLCodeNext
    override val bottomLine: View
        get() = viewBinding.vPhoneDLBottomLine
    override val phoneNextAnim: LottieAnimationView
        get() = viewBinding.lottiePhoneDLPhoneNextAnim
    override val resendCodeAnim: LottieAnimationView
        get() = viewBinding.lottiePhoneDLResendCodeAnim
    override val codeNextAnim: LottieAnimationView
        get() = viewBinding.lottiePhoneDLCodeNextAnim
    override val phoneNextContainer: LinearLayout
        get() = viewBinding.linearPhoneDLPhoneNextContainer
    override val codeNextContainer: LinearLayout
        get() = viewBinding.linearPhoneDLCodeNextContainer
    override val codeContainer: ConstraintLayout
        get() = viewBinding.conPhoneDLCodeContainer
    override val tvMiddleTitle: TextView?
        get() = viewBinding.viewTitle.tvMiddleTitle
    override val tvRightTitle: TextView?
        get() = viewBinding.viewTitle.tvRightTitle
    override val ivLeftImage: ImageView?
        get() = viewBinding.viewTitle.ivBackIcon
    override val viewStatusHeight: View?
        get() = viewBinding.viewTitle.viewStatusHeight

    override fun onDestroy() {
        super.onDestroy()
        codeKeyboardUtils.unregisterKeyboardHeightListener(this@PhoneLoginActivity)
        phoneKeyboardUtils.unregisterKeyboardHeightListener(this@PhoneLoginActivity)
        if (phoneNextAnim != null) {
            phoneNextAnim.cancelAnimation()
        }
        if (resendCodeAnim != null) {
            resendCodeAnim.cancelAnimation()
        }
        if (codeNextAnim != null) {
            codeNextAnim.cancelAnimation()
        }
    }
}